From 9dc77cdca0c57dd749437a4784b3231433429c2a Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 5 May 2024 01:11:10 -0700
Subject: fix(badges): maintain pinned category order

---
 src/routes/user/[user]/badges/+page.svelte | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

(limited to 'src/routes/user/[user]/badges/+page.svelte')

diff --git a/src/routes/user/[user]/badges/+page.svelte b/src/routes/user/[user]/badges/+page.svelte
index fc938b21..772cfdfe 100644
--- a/src/routes/user/[user]/badges/+page.svelte
+++ b/src/routes/user/[user]/badges/+page.svelte
@@ -324,14 +324,15 @@
 		return Object.entries(groupedBadges)
 			.sort((a, b) => a[1].length - b[1].length)
 			.sort((a, b) => {
-				if (preferences.pinned_badge_wall_categories) {
-					const pinnedCategories = preferences.pinned_badge_wall_categories.split(',');
+				const pinnedCategories = preferences.pinned_badge_wall_categories || ([] as string[]);
+				const aIndex = pinnedCategories.indexOf(a[0]);
+				const bIndex = pinnedCategories.indexOf(b[0]);
 
-					if (pinnedCategories.includes(a[0]) && !pinnedCategories.includes(b[0])) return -1;
-					if (pinnedCategories.includes(b[0]) && !pinnedCategories.includes(a[0])) return 1;
-				}
+				if (aIndex === -1 && bIndex === -1) return 0;
+				if (aIndex === -1) return 1;
+				if (bIndex === -1) return -1;
 
-				return 0;
+				return aIndex - bIndex;
 			})
 			.reduce((set: GroupedBadges, [key, value]) => {
 				set[key] = value;
-- 
cgit v1.2.3

